// pages/confirm/confirm.js
const util = require("../../../utils/util.js");
import {ajax} from "../../../utils/httpRequest";
import {URI_SENDCODE, URI_REGISTER} from "../../../utils/api";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tel: '',
    captcha: '',
    code: '',
    captchacode: '',
    id: '',
    type: '',
    randStr: '',
    tosend: false,
    tips: '获取验证码',
    durationtime: 60,
    timer: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var id = util.randomStr(4);
    var yan_img = util.verification(id);
    this.setData({
      id: id,
      captcha: yan_img
    });
    //console.log(this.data.id,this.data.captcha);
  },

  telInput: function (e) {
    this.setData({
      tel: e.detail.value
    });
  },

  codeInput: function (e) {
    this.setData({
      code: e.detail.value
    });
  },

  getCaptchaCode: function (e) {
    this.setData({
      captchacode: e.detail.value
    });
  },


  //点击图形验证码 改变验证码
  changeCode: function () {
    var id = util.randomStr(4);
    var yan_img = util.verification(id);
    this.setData({
      id: id,
      captcha: yan_img
    });
    //console.log(this.data.id,this.data.captcha);
  },

  //获取手机验证码
  getConirm: function () {
    if (!this.data.tel) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else {
      const TEL_REGEXP = /^1([38]\d|5[0-35-9]|7[3678])\d{8}$/;
      if (!TEL_REGEXP.test(this.data.tel)) {
        wx.showToast({
          title: '手机号输入有误',
          icon: 'none',
          duration: 2000
        });
      } else {
        var that = this;
        this.setData({
          tosend: !this.data.tosend,
          tips: this.data.durationtime + 's后重试'
        });
        var timer = null;
        //验证码倒计时
        timer = setInterval(function () {
          console.log(that.data.durationtime)
          if (that.data.durationtime > 0) {
            that.setData({
              durationtime: that.data.durationtime - 1,
              tips: that.data.durationtime + 's后重试'
            })
          } else {
            clearInterval(timer);
            that.setData({
              tips: '获取验证码',
              durationtime: 10,
              tosend: false
            })
          }

        }, 1000);
        ajax.get(URI_SENDCODE, {
          tel: this.data.tel
        }).then(res => {
          this.setData({
            randStr: res.data.randStr
          });
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          })
        })
      }
    }
  },


  //下一步 信息录入
  goToRecord: function () {
    clearInterval(this.data.timer);
    ajax.post(URI_REGISTER, {
      tel: this.data.tel,
      captcha: this.data.captchacode,
      code: this.data.code,
      id: this.data.id,
      type: 3,
      unionid: wx.getStorageSync('only_u')
    }).then(res => {
      console.log(res);
      if (res.code == 1) {
        wx.setStorageSync('tel', this.data.tel);
        wx.redirectTo({
          url: '../input/input'
        })
      } else if (res.code == 8) {
        wx.showModal({
          title: '温馨提示',
          content: '手机号已注册绑定成功',
          showCancel: false,
          confirmColor: '#0d77e4',
          success: (res) => {
            if (res.confirm) {
              wx.redirectTo({
                url: '/pages/entry/authorize/authorize',
              })
            }
          }
        });
      }

    })
  }


})